package aad.storyboard.controller;

import java.util.ArrayList;

import aad.storyboard.R;
import aad.storyboard.model.AnimationProvider;
import aad.storyboard.model.Chapter;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Story;
import aad.storyboard.model.menu.MenuItem;
import aad.storyboard.view.MenuViewCreator;
import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MenuTouchListener implements OnTouchListener, OnSeekBarChangeListener, OnCheckedChangeListener{

	private static MenuTouchListener instance = new MenuTouchListener();
	private Activity activity;
	private MenuViewProvider menuViewProvider;
	
	private MenuTouchListener(){
		
	}
	
	public static MenuTouchListener getInstance(Activity act, MenuViewProvider viewProvider){
		instance.activity = act;
		instance.menuViewProvider = viewProvider;
		return instance;
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int id = v.getId();
		int number = -1;
		if(v.getTag() != null)
			number = ((Integer) v.getTag()).intValue();
		System.out.print("id: "+id);
		View nextView = null;
		switch(id){
		
			case MenuItem.Action.NONE: 
				break;
			
			case MenuItem.Action.EXIT: 
				System.exit(0);
				break;
			
			case MenuItem.Action.CHAPTER:
				System.out.println("CHAPTERSELECTOION");
				nextView = menuViewProvider.getChapterMenuView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				
				activity.setContentView(nextView);
				break;
				
				
			case MenuItem.Action.CHAPTERSELECTION:
				System.out.println("CHAPTER");
				nextView = menuViewProvider.getSceneMenuView(number);
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				
				activity.setContentView(nextView);
				break;
				
			case MenuItem.Action.SCENE:
				ArrayList<Chapter> chapters = Story.getInstance().getChapters();
				Chapter chapter = null;
				Scene scene = null;
				for(Chapter c : chapters){
					for(Scene s : c.getScenes()){
						if(s.getId() == number)
							chapter = c;
							scene =s;
					}
				}
				if(scene != null){
					nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
					if(nextView.getParent() != null){
						((ViewGroup)nextView.getParent()).removeView(nextView);
					}
					
					activity.setContentView(nextView);
					//TODO start from that particular scene
				}
				break;
				
		
			case MenuItem.Action.MAIN_MENU:
				AudioPlayer.getInstance().stopAudio();
				if(Story.getInstance().getCurrentChapter() != null && Story.getInstance().getCurrentChapter().getCurrentScene() != null){
					AnimationProvider.getInstance().stopAllActiveAnimations();
					AnimationProvider.getInstance().undoAllAnimationsBackToTime(Story.getInstance().getCurrentChapter().getCurrentScene().getStartTime());
				}
				View mainMenuView = menuViewProvider.getMainMenuView();
				activity.setContentView(mainMenuView);
				break;
			
			case MenuItem.Action.RESTART:
				AudioPlayer.getInstance().stopAudio();
				AnimationProvider.getInstance().stopAllActiveAnimations();
				AnimationProvider.getInstance().undoAllAnimationsBackToTime(Story.getInstance().getCurrentChapter().getCurrentScene().getStartTime());
				AudioPlayer.getInstance().seekToPosition(0);
				nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(ControllerManager.getInstance().getGuiComposer().getMainView());
				ControllerManager.getInstance().startStory();
				break;
			
			case MenuItem.Action.START:
				//Log.e("Parent", " I already have a parent "+ ControllerManager.getInstance().getGuiComposer().getMainView().getParent().toString());
				nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				ControllerManager.getInstance().startStory();
				break;
				
			case MenuItem.Action.RESUME:
				AudioPlayer.getInstance().playAudio();
				AnimationProvider.getInstance().unpauseAllAnimations();
				nextView = ControllerManager.getInstance().getGuiComposer().getMainView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				break;
				
			case MenuItem.Action.SETTINGS:
				AudioPlayer.getInstance().stopAudio();
				AnimationProvider.getInstance().stopAllActiveAnimations();
				nextView = menuViewProvider.getSettingsMenuView();
				if(nextView.getParent() != null){
					((ViewGroup)nextView.getParent()).removeView(nextView);
				}
				activity.setContentView(nextView);
				break;
				
		}
		return false;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		// TODO Auto-generated method stub
		
	}

}
